/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 * 
 * The contents of this file are subject to the Mozilla Public License Version 
 * 1.1 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License specifically applicable to 
 * this software at http://developer.yahoo.com/yslow/license.txt, or the general 
 * form at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 * 
 * The Original Code is: YSlow
 * 
 * The Initial Developer of the Original Code is Yahoo! Inc.
 * 
 * Copyright (C) 2007, Yahoo! Inc. All Rights Reserved.
 * 
 * Contributor(s): 
 * ***** END LICENSE BLOCK ***** */

//Tools Class object in the YSLOW namespace.
if ( typeof YSLOW.Tools == "undefined") {
	YSLOW.Tools = {};
}

// Combine all the JS into one window.
YSLOW.Tools.loadAllJS = function() {
	var sHtml = "";
	var sJSCode = "";

	// Iterate over the external JS files.
	var aComponents = FirebugContext.yslowContext.getComponents("js");
	for ( var i = 0; i < aComponents.length; i++ ) {
		var compObj = aComponents[i];
		if ( compObj['responseText'] ) {
			sJSCode += "\n\n\n////////////////////////////////////////////////////////////////////////////////\n// " + 
					   compObj['url'] + "\n////////////////////////////////////////////////////////////////////////////////\n\n" + 
                       compObj['responseText'];
		}
	}

	// Iterate over the inline SCRIPT blocks.
	var aScripts = FirebugContext.yslowContext.document.getElementsByTagName("script");
	var iScript = 0;
	for ( var i = 0; i < aScripts.length; i++ ) {
		var script = aScripts[i];
		if ( !script.src ) {  // avoid external script objects
			iScript++;
			sJSCode += "\n\n\n////////////////////////////////////////////////////////////////////////////////\n// " + 
				       "inline script block #" + iScript + "\n////////////////////////////////////////////////////////////////////////////////\n\n";
			sJSCode += script.innerHTML;
		}
	}

	sHtml += "<b><font style='font-family: Arial;'><font style='font-size: 2em;'>// All JavaScript for: </font>" + FirebugContext.yslowContext.document.location + "</font></b>\n";
	sHtml += "<pre>" + YSLOW.Utils.escapeHtml(sJSCode) + "</pre>";

	FirebugContext.yslowContext.alljswindow = window.open("", "_blank");
	if ( ! FirebugContext.yslowContext.alljswindow ) {
		alert("ERROR: Problem opening All JS window.");
		return;
	}

	sHtml = 
'<html>\n' +
'<title>All JavaScript for: ' + FirebugContext.yslowContext.document.location.href.substring(0, 25) + '</title>' +
'<head>\n' +
'<scr' + 'ipt>' +
'YSLOW_NO = true;' +
'</scr' + 'ipt>' +
'</head>\n' +
'' +
'<body>\n' +
'<h1 style="font-family: Arial;">/* YSlow for Firebug by Yahoo! */</h1>'+
sHtml +
'</body>\n' +
'</html>\n' +
'';

	// Here's the way to open it in a new tab:
	//FirebugContext.getPanel('yslow').openNewTab();
	//gBrowser.contentdocument.open();
	//gBrowser.contentdocument.writeln(sHtml);
	//gBrowser.contentdocument.close();

	FirebugContext.yslowContext.alljswindow.document.open();
	FirebugContext.yslowContext.alljswindow.document.writeln(sHtml);
	FirebugContext.yslowContext.alljswindow.document.close();
	
}


// Combine all the CSS into one window.
YSLOW.Tools.loadAllCSS = function() {
	var sHtml = "";
	var sCode = "";

	// Iterate over the external files.
	var aComponents = FirebugContext.yslowContext.getComponents("css");
	for ( var i = 0; i < aComponents.length; i++ ) {
		var compObj = aComponents[i];
		if ( compObj['responseText'] ) {
			sCode += 
			"\n\n\n/*******************************************************************************\n  " + 
					   compObj['url'] +
					   "\n*******************************************************************************/\n\n" + 
                       compObj['responseText'];
		}
	}

	// Iterate over the inline STYLE blocks.
	var aElements = FirebugContext.yslowContext.document.getElementsByTagName("style");
	var bFirstInline = true;
	for ( var i = 0; i < aElements.length; i++ ) {
		var elem = aElements[i];
		if ( elem.innerHTML ) {  // avoid external elem objects
			if ( bFirstInline ) {
				sCode += "\n\n\n/************************************************\n " + 
					       " inline style blocks" + "\n************************************************/\n\n";
				bFirstInline = false;
			}
			sCode += elem.innerHTML;
		}
	}

	sHtml += "<b><font style='font-family: Arial;'><font style='font-size: 2em;'>/* All CSS for: </font>" + FirebugContext.yslowContext.document.location + "</font><font style='font-family: Arial;'><font style='font-size: 2em;'> */</font></b>\n";
	sHtml += "<pre>" + sCode + "</pre>";

	FirebugContext.yslowContext.allcsswindow = window.open("", "_blank");
	if ( ! FirebugContext.yslowContext.allcsswindow ) {
		alert("ERROR: Problem opening All CSS window.");
		return;
	}

	sHtml = 
'<html>\n' +
'<title>All CSS for: ' + FirebugContext.yslowContext.document.location.href.substring(0, 25) + '</title>' +
'<head>\n' +
'<scr' + 'ipt>' +
'YSLOW_NO = true;' +
'</scr' + 'ipt>' +
'</head>\n' +
'' +
'<body>\n' +
'<h1 style="font-family: Arial;">/* YSlow for Firebug by Yahoo! */</h1>'+
sHtml +
'</body>\n' +
'</html>\n' +
'';

	FirebugContext.yslowContext.allcsswindow.document.open();
	FirebugContext.yslowContext.allcsswindow.document.writeln(sHtml);
	FirebugContext.yslowContext.allcsswindow.document.close();
}


YSLOW.Tools.printable = function() {
	FirebugContext.yslowContext.bPrintable = true;
	var win = window.open("", "_blank");
	win.document.open();
	win.document.write(
		'<html><head>\n' +
		'<script>YSLOW_NO = true;</script>' +   // prevent YSlow? Greasemonkey plugin from scanning this popup window
		
		//CSS
		'<link href="chrome://yslow/content/yslow.css" type="text/css" rel="stylesheet">'+
		'<style type="text/css">'+
		'#borderDiv'+
		'{'+
		'	background: #FFFFFF;'+
		'}'+
		'</style>'+
		'</head><body style="font-size: 10pt; font-family: Arial; background: #EBEBE9;">' +
		
		//Header
		'<div style=\" font-size: 1.5em; color: #E05000; margin: 2px;\"><img style=\"padding-right: 5px\"  src=\"chrome://yslow/content/logo_32x32.png\" align=\"absbottom\"/>YSlow for Firebug</div>'+
		'<hr />'+
		
		//Print the website address:
		'<div style="margin-bottom: 10px; font-size: 1.5em;">' +
		'<span>URL:</span>' + 
		'<span style="margin-left: 10px">' + YSLOW.Utils.prettyAnchor(FirebugContext.window.document.location.href,FirebugContext.window.document.location.href) + '</span>' +
		'</div>'+
		
		'<div id=yslowDiv>'+	//yslowDiv start
		
		// performance
		'<div class=peelerText style="font-size: 1.5em;" style=\"margin-top:  10px\">Performance</div>' +
		'<div id=borderDiv class=peelerDiv style="width: auto; height: auto; position: relative; margin-bottom: 20px; border: 4px #F0F0F0 ridge; padding-bottom: 10px;">\n' +
		FirebugContext.yslowContext.genPerformance() +
		'</div>\n' +

		// stats
		'<div class=peelerText style="font-size: 1.5em;">Stats</div>' +
		'<div id=borderDiv class=peelerDiv style="width: auto; height: auto; position: relative; margin-bottom: 20px; border: 4px #F0F0F0 ridge; padding-bottom: 10px;">\n' +
		FirebugContext.yslowContext.genStats() +
		'</div>\n' +

		// components
		'<div class=peelerText style="font-size: 1.5em;">Components</div>' +
		'<div id=borderDiv class=peelerDiv style="width: auto; height: auto; position: relative; margin-bottom: 20px; border: 4px #F0F0F0 ridge; padding-bottom: 10px;">\n' +
		FirebugContext.yslowContext.genComponents() +
		'</div>\n' +
		'</div>\n' + //yslowDiv END
		'<br /><hr />'+
		'<div style=\"text-align: center; font-size: 10pt; margin-top: 20px;\">Copyright &copy; 2007 Yahoo! Inc. All rights reserved.</div>'+
		'</body></html>\n');
	win.document.close();
	
	//If we were generating performance stats for a printable version, we should reset this flag after we are done. 
	//This is required so that performance output on the FF panel remains unexpanded and correctly formated.
	FirebugContext.yslowContext.bPrintable = false;
}
